package com.qinggeng.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.web.page.PageDomain;
import com.qinggeng.project.api.dto.Classify;
import com.qinggeng.project.system.domain.GoodsClassify;
import com.qinggeng.project.system.domain.GoodsLowerClassify;
import com.qinggeng.project.system.domain.vo.GoodsLowerClassifyVO;
import com.qinggeng.project.system.dto.GoodsLowerClassifyDTO;
import com.qinggeng.project.system.mapper.GoodsClassifyMapper;
import com.qinggeng.project.system.mapper.GoodsLowerClassifyMapper;
import com.qinggeng.project.system.service.IGoodsLowerClassifyService;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsLowerClassifyServiceImpl implements IGoodsLowerClassifyService {

    @Autowired
    private GoodsLowerClassifyMapper goodsLowerClassifyMapper;

    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;



    @Override
    public boolean insertGoodsLowerClassify(GoodsLowerClassify goodsLowerClassify) {
        goodsLowerClassify=this.checkGoodsLowerClassify(goodsLowerClassify);
        int size=goodsLowerClassifyMapper.insertSelective(goodsLowerClassify);
        return size>=1;
    }

    private GoodsLowerClassify  checkGoodsLowerClassify(GoodsLowerClassify goodsLowerClassify){
        if (null==goodsLowerClassify){
            throw new CustomException("GoodsLowerClassify 不能为空");
        }
        Long classifyId = goodsLowerClassify.getClassifyId();
        if (null== classifyId){
            throw new CustomException("classifyId 不能为空");
        }
        GoodsClassify goodsClassify=goodsClassifyMapper.selectGoodsClassifyById(classifyId);
        if (null ==goodsClassify ){
            throw new CustomException("找不到商品分类");
        }
        String img=goodsLowerClassify.getClassifyImg();
        if (StringUtils.isEmpty(img)){
            throw new CustomException("图片地址不能为空");
        }
        String url=goodsLowerClassify.getUri();

        if (StringUtils.isEmpty(url)){
            throw new CustomException("参数不能为空");
        }
        return  goodsLowerClassify;
    }


    @Override
    public boolean updateGoodsLowerClassify(GoodsLowerClassifyDTO dto) {
        Long id=dto.getId();
        goodsLowerClassifyMapper.deleteByPrimaryKey(id);
        return  addGoodsLowerClassify(dto);
    }

    @Override
    public boolean addGoodsLowerClassify(GoodsLowerClassifyDTO dto) {
        GoodsLowerClassify g=(GoodsLowerClassify)dto;
        g=this.checkGoodsLowerClassify(g);
        String name=dto.getName();
        GoodsClassify goodsClassify=new GoodsClassify();
        goodsClassify.setClassifyName(name);
        goodsClassifyMapper.insertGoodsClassify(goodsClassify);
        g.setClassifyId(goodsClassify.getClassifyId());
        int size=goodsLowerClassifyMapper.insertSelective(g);
        return size>=1;
    }
    private void checkGoodsLowerClassifyById(Long id){
        if(null==id){
            throw new CustomException("id不能为空");
        }
        GoodsLowerClassify lowerClassify=goodsLowerClassifyMapper.selectByPrimaryKey(id);
        if (null==lowerClassify){
            throw new CustomException("记录不存在");
        }
    }

    @Override
    public boolean deleteGoodsLowerClassify(Long id) {
        final GoodsLowerClassify goodsLowerClassify = new GoodsLowerClassify();
        goodsLowerClassify.setId(id);
        checkGoodsLowerClassifyById(id);
        goodsLowerClassify.setDelFalg(0);
        int sixe=goodsLowerClassifyMapper.updateByPrimaryKeySelective(goodsLowerClassify);
        return sixe>=1;
    }

    @Override
    public List<GoodsLowerClassify> listGoodsLowerClassify(Long classifyId) {
        Example example =new Example(GoodsLowerClassify.class);
        example.createCriteria()
                .andEqualTo("delFalg",10)
                .andEqualTo("visible",0)
                .andEqualTo("classifyId",classifyId);
        List<GoodsLowerClassify>  goodsLowerClassifies=goodsLowerClassifyMapper.selectByExample(example);
        if (null==goodsLowerClassifies){
            goodsLowerClassifies=new ArrayList<>();
        }
        return goodsLowerClassifies;
    }

    @Override
    public List<GoodsLowerClassify> listAll(PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());
        List<GoodsLowerClassify>  goodsLowerClassifies=goodsLowerClassifyMapper.selectAll();
        return goodsLowerClassifies;
    }

    @Override
    public List<GoodsLowerClassifyVO> listGoodsLowerClassifyVO(Long classId) {
        List<GoodsLowerClassifyVO> goodsLowerClassifyVOS=new ArrayList<>();
        List<GoodsLowerClassify>  goodsLowerClassifies= listGoodsLowerClassify(classId);
        for (GoodsLowerClassify goodsLowerClassify:goodsLowerClassifies) {
            GoodsLowerClassifyVO goodsLowerClassifyVO=new GoodsLowerClassifyVO();
            goodsLowerClassifyVO.setUrl(goodsLowerClassify.getClassifyImg());
            goodsLowerClassifyVO.setCode(String.valueOf(goodsLowerClassify.getCode()));
            goodsLowerClassifyVO.setArg(goodsLowerClassify.getUri());
            goodsLowerClassifyVO.setIsReturn(String.valueOf(goodsLowerClassify.getIsReturn()));
            goodsLowerClassifyVOS.add(goodsLowerClassifyVO);
        }
        return goodsLowerClassifyVOS;
    }



}
